package com.bitinstitute.parkapp.home;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.bitinstitute.parkapp.R;
import com.bitinstitute.parkapp.helper.TabGroupActivity;

/**
 * Displays chosen News from User
 */
public class Price extends Activity {

	static final String url = "http://www.luisenpark.de/mein-luisenpark/eintrittspreise";
	/**
	 * View of Website
	 */
	private WebView webView;
	/**
	 * Loading Dialog which informs the User that the website is loading
	 * 
	 */
	private ProgressDialog dialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		setContentView(R.layout.home_displayweb);
		
		final TextView myTitleText = (TextView) findViewById(R.id.headerText);
	        myTitleText.setText("Eintrittspreise");

		// Allocation of WebView
		webView = (WebView) findViewById(R.id.webView);

		dialog = new ProgressDialog((TabGroupActivity) getParent());
		dialog.setTitle("Bitte warten Sie!");
		dialog.setMessage("Die Webseite wird geladen.");

		webView.setWebViewClient(new WebViewClient() {

			// runs when the site starts to load
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {

				// show the waiting dialog
				dialog.show();
			}

			// when site has finished loading
			@Override
			public void onPageFinished(WebView view, String url) {

				// if the dialog is still there he will be closed
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
			}

			// runs when User uses extern links on website
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {

				// website will be displayed in the Webview
				view.loadUrl(url);
				return true;
			}
		});

		// loading Url
		webView.loadUrl(url);

		
	   // Back Button 
	       
		findViewById(R.id.btnBack).setOnClickListener(new OnClickListener() {
			 
	        public void onClick(View v) {

	        	dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_BACK));

				dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
						KeyEvent.KEYCODE_BACK));

	        }
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		// when the back-button will be pressed and theres a History available of visited websites
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
			webView.goBack();
			return true;
		}

		// If the if-request = false, then go back to the last event behind the pressed button
		return super.onKeyDown(keyCode, event);
	}
}
